﻿using BluetoothMachine.Domains;
using BluetoothMachine.Enums;
using BluetoothMachine.ExpandHelper;
using BluetoothMachine.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace BluetoothMachine.GlobalStatics
{

    /// <summary>
    /// 全局用户数据
    /// </summary>
    [Attributes.Singleton]
    public class UserGlobal
    {
        public ObservableCollection<UserModel> UserList { get; set; }
        public UserModel User { get; set; }
        public AuthorityType LoginAuthority { get; set; } = AuthorityType.管理员;

        public UserGlobal()
        {
            User = new UserModel() { Level = Enums.AuthorityType.Null };
            if (!File.Exists(FixedParameter.UserPath))
            {
                UserList = new ObservableCollection<UserModel>() {
                    new UserModel()
                    {
                        Level = AuthorityType.管理员,
                        Name = "admin",
                        Password = "admin999",
                    },
                };
            }
            else UserList = JsonSerializer.Deserialize<ObservableCollection<UserModel>>(DES.DESDecrypt(File.ReadAllText(FixedParameter.UserPath, Encoding.UTF8), FixedParameter.DES_KEY, FixedParameter.DES_IV));
        }

        public void Save()
        {
            UserModel model = UserList.FirstOrDefault(x => x.Name == User.Name);
            if (model == null && User.Name != "admin")
            {
                UserList.Add(new UserModel()
                {
                    Address = User.Address,
                    Shift = User.Shift,
                    Level = AuthorityType.普工,
                    Name = User.Name,
                    Password = User.Password,
                });
            }
            else
            {
                User.UpdateTime = DateTime.Now;
                User.UpdateValue(model);
            }
            SaveText();
        }

        public void Delect()
        {
            UserModel model = UserList.FirstOrDefault(x => x.Name == User.Name && x.Name != "admin");
            if (model != null)
            {
                UserList.Remove(model);
                SaveText();
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="passworid">密码</param>
        /// <returns></returns>
        public async Task<bool> Login(string name, string passworid)
        {
            var model = UserList.FirstOrDefault(x => x.Name == name && x.Password == passworid);
            if (model != null)
            {
                LoginAuthority = model.Level;
                //Bootstrapper.GetContainer().ResolveSingleton<MainGlobal>().UpdateMenu();
                //SaveText();
                return true;
            }
            return false;
        }

        private void SaveText() => File.WriteAllText(FixedParameter.UserPath, DES.DESEncrypt(JsonSerializer.Serialize(UserList), FixedParameter.DES_KEY, FixedParameter.DES_IV), Encoding.UTF8);
    }
}
